package com.hrg.demo.config;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 将本地文件系统路径映射为Web可访问的URL
 *
 * 浏览器请求: /local-images/photos/image.jpg
 * Spring MVC 匹配到 /local-images/** 模式
 * 将请求映射到 file:D:/photos/image.jpg
 * 返回图片文件内容
 * @author shenzq
 */
@Configuration
@RequiredArgsConstructor
public class WebConfig implements WebMvcConfigurer {

    private final ImagesConfig imagesConfig;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 将 D:/images 目录映射为Web可访问的URL
//        registry.addResourceHandler("/local-images/**")
//                .addResourceLocations("file:D:/images/");

        registry.addResourceHandler("/"+imagesConfig.getLocalImages()+"/**")
                .addResourceLocations("file:"+imagesConfig.getImageBaseDir()+"/");
    }
}
